(() => {
  let ids = location.search.split("?")[1];
  let ele = {};
  ele.bot = $("#bot");
  ele.bots = $("#bots");
  ele.chahao4 = $("#chahao4");
  ele.btn_que4 = $("#btn_que4");
  ele.modal_tenant = $("#modal-tenant");
  ele.modal_income = $("#modal-income");
  ele.more = $("#more");
  ele.empty = $("#empty");
  ele.chahao5 = $("#chahao5");
  ele.list_tou = $("#list_tou");
  ele.weijin = $("#weijin");
  ele.leijin = $("#leijin");
  ele.yijian = $("#yijian");
  let obj = {};
  let list = [];
  let page = 1;

  // todo点击×就×掉
  ele.chahao4.onclick = () => {
    closeMdl(ele.modal_income);
  };
  // todo点击确认关闭
  ele.btn_que4.onclick = () => {
    closeMdl(ele.modal_income);
  };
  // todo点击一键领取
  ele.yijian.onclick = () => {
    reward();
  };
  // };
  // 点击差号
  ele.chahao5.onclick = () => {
    closeMdl(ele.modal_tenant);
  };
  // !发请求

  asyncList(page);
  function asyncList(page) {
    let token = getCookie("token");
    ajax({
      method: "get",
      url: API_PATH.RENT,
      header: {
        Authorization: token,
      },
      data: {
        page: page,
        size: 20,
        h_id: ids,
      },
      success: (res) => {
        if (res.code == 1) {
          obj = res.data.detail;
          list = [...list, ...res.data.list.data];
          if (list.length === 0) {
            page = 0;
            ele.more.style.display = "none";
            ele.empty.style.display = "block";
          } else {
            initLisy(list);
            page++;
            if (page > res.data.list.last_page) {
              ele.more.innerText = "没有更多了";
              ele.more.onclick = null;
            } else {
              ele.more.innerText = "加载更多";
              ele.more.onclick = () => asyncList(page);
            }
          }
        } else openTips(res.msg);
        // obj = res.data;
        // list = [...list, ...obj.list.data];
        // initLisy();
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }
  // !炫数据
  function initLisy(lists) {
    let newList = lists
      .map((item, index) => {
        return `
      <div class="bot_list">
      <div class="left">${item.created_at.split(" ")[0]}</div>
      <div class="left_c">${item.info.name}${item.l_id}号</div>
      <div class="right_r">
        <img src="http://img.qxgs.cc/tiantianlaishouzu/img/public/jinbi.png" alt="" /><span>${
          item.money
        }</span>
      </div>
      <div>
      <p id="xiangqing" data-money="${item.one_money}" data-time=${
          item.created_at.split(" ")[1]
        } data-id="${index}" style="display:${
          item.type == 1 ? "block" : "none"
        };" class="top_l">租金详情></p>
      <div data-id="${item.id}" id="lingqu" class="${
          item.state == 0 ? "right" : "right hui"
        }">${item.state == 0 ? "领取" : "已领取"}</div>
      </div>
    </div>`;
      })
      .join(" ");
    ele.bots.innerHTML = newList;

    // !获取详情按钮点击打开弹窗
    let xiangqingAll = document.querySelectorAll("#xiangqing");
    xiangqingAll.forEach((item) => {
      item.onclick = () => {
        let arr = lists[item.dataset.id];

        let newArr = arr.user_list
          .map((ite) => {
            return ` <div class="list_son">
          <div class="left_img">
            <div class="img">
              <img src="${
                ite.head_pic
                  ? IMG_DOMAIN + ite.head_pic
                  : ite.size == 1
                  ? "http://img.qxgs.cc/tiantianlaishouzu/img/my/tx1.png"
                  : "http://img.qxgs.cc/tiantianlaishouzu/img/my/tx2.png"
              }" alt="" />
            </div>
            <div class="name_fang">${
              ite.nick_name ? ite.nick_name : "天天来收租"
            }</div>
          </div>
          <div class="time">${item.dataset.time}</div>
          <div class="jin">
            <img src="http://img.qxgs.cc/tiantianlaishouzu/img/public/jinbi.png" alt="" />${
              item.dataset.money
            }
          </div>
        </div>`;
          })
          .join(" ");

        // 打开弹窗
        ele.list_tou.innerHTML = newArr;
        openMdl(ele.modal_tenant);
      };
    });
    // !点击领取按钮
    let lingquAll = document.querySelectorAll("#lingqu");
    lingquAll.forEach((item) => {
      item.onclick = () => {
        reward(item.dataset.id);
      };
    });
    // !渲染下方的总数
    ele.leijin.innerHTML = obj.all_zu_money;
    ele.weijin.innerHTML = obj.zu_money;
  }

  function reward(id = " ") {
    let token = getCookie("token");
    ajax({
      method: "post",
      url: API_PATH.REWARDS,
      header: {
        Authorization: token,
      },
      data: {
        id: id,
      },
      success: (res) => {
        openTips(res.msg);
        list = [];
        page = 1;
        asyncList(page);
      },
      error: (err) => {
        // openTips(err.msg);
      },
    });
  }
})();
